import { Component, Input, Output, EventEmitter, OnChanges} from '@angular/core';
import {PagerService} from "./pager.service";

@Component({
  selector: 'app-pagination',
  templateUrl: './page.component.html',
  styleUrls: ['./page.component.css'],
  providers: [PagerService]
})
export class PageComponent implements OnChanges {

  @Input() totalItems: number;
  @Input() currentPage: number;
  @Input() pageSize: number;
  @Output() onReGetPage = new EventEmitter<any>();
  setPage(toPageNum) {
    if (toPageNum <= 1 && this.pager.currentPage === 1) {
      return;
    }
    if (toPageNum >= this.pager.totalPages && this.pager.currentPage === this.pager.totalPages) {
      return;
    }
    this.onReGetPage.emit(toPageNum);
  }
  pager: any;
  constructor(private pagerService: PagerService) { }
  ngOnChanges() {
    this.pager = this.pagerService.getPager(this.totalItems,this.currentPage,this.pageSize);
  }

}
